package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Transactional
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private RedisTemplate<Object , Object>redisTemplate;
    private static final String CACHE_KEY_PREFIX = "nursingPlan:all";

    /*清除缓存*/
    private void deleteCache() {
        redisTemplate.delete(CACHE_KEY_PREFIX);
    }



    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
//        1.根据id查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo=new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
//        根据护理计划id查询关联的所有护理项目
        List<NursingProjectPlanVo> list=nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {



            // 1. 从缓存中获取数据
            List<NursingPlan> list = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);

            if (list == null || list.isEmpty()) {
                // 2. 缓存为空，从数据库查询
                list = nursingPlanMapper.selectNursingPlanList(nursingPlan);

                if (list != null && !list.isEmpty()) {
                    // 3. 将数据库查询结果写入缓存
                    redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, list);
                }
            }

            return list;


        // return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto dto)
    {
        //保存护理计划
        //属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto,nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        //批量保存护理项目计划关系
        int count = nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(),nursingPlan.getId());
        deleteCache(); // 清除缓存
        return count == 0?0:1;
    }

    /**
     * 修改护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto dto)
    {
        try {
            //属性拷贝
            NursingPlan nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(dto,nursingPlan);
            //判断dto中的项目列表为空，如果不为空，则先删除护理计划与护理项目的关系，然后重新批量添加
            if(dto.getProjectPlans()!=null&&!dto.getProjectPlans().isEmpty()){
                //删除护理计划对应的护理项目列表
                nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
                //补全护理计划id
                List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
                projectPlans.forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(nursingPlan.getId()));
                //批量保存护理计划对应的护理项目列表
                nursingProjectPlanMapper.batchInsert(projectPlans,nursingPlan.getId());
            }
            //不管项目是否为空都要修改护理计划
            int result = nursingPlanMapper.updateNursingPlan(nursingPlan);
            if (result > 0) {
                deleteCache(); // 补全：更新后清除缓存
            }
            return result;
           // return nursingPlanMapper.updateNursingPlan(nursingPlan);
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        boolean result = removeByIds(Arrays.asList(ids));
        if (result) {
            deleteCache(); // 补全：删除后清除缓存
        }
        return result ? 1 : 0;
       // return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        boolean result = removeById(id);
        if (result) {
            deleteCache(); // 补全：删除后清除缓存
        }
        return result ? 1 : 0;
        /*//删除护理计划和护理项目的关系
        nursingProjectPlanMapper.deleteByPlanId(id);
        //删除护理计划
        return removeById(id) ? 1 : 0;*/
    }

  /*  @Override
    public List<NursingPlan> listAll() {
        // 从缓存中获取
        List<NursingPlan> list = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);

        // 缓存中有数据，直接返回
        if (list != null && !list.isEmpty()) {
            return list;
        }

        // 缓存中没有数据，从数据库中查询
        LambdaQueryWrapper<NursingPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NursingPlan::getStatus, 1); // 假设状态为1表示启用
        list = nursingPlanMapper.selectList(wrapper);

        // 将数据写入缓存
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, list);

        return list;
    }*/

}
